<template>
<view>
<!--hotmall/packageA/login/login.wxml-->
<!-- <text>hotmall/packageA/login/login.wxml</text> -->
<!-- hidden="{{loginModelHidden}}" -->
<view class="loginModel">
        <image class="loginImg" src="/static/remao_shop/img/login.png"></image>
        <view class="loginText">我们需要获得您的授权，以继续操作</view>
        <button @getuserinfo="updateUserInfo" class="loginBtn" openType="getUserInfo">
            <image class="loginBtnimg" src="/static/remao_shop/img/weixin.png"></image>微信授权登录</button>
    </view>
    <view class="loginBg" :hidden="loginModelHidden"></view>
</view>
</template>

<script>
import api from "../../../api";
var app = getApp();

export default {
  data() {
    return {
      userId: '',
      img: '',
      name: 'userNickName',
      getList: [],
      loginModelHidden: !0,
      code: 0,
      integList: '0'
    };
  },

  components: {},
  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {// this.we7login();
    // let userId = wx.getStorageSync("userId")
    // this.remaologin()
    // this.setData({
    //   userId: userId
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  methods: {
    // // 微擎登录
    // we7login(e) {
    //   var t = this,
    //     a = wx.getStorageSync("userInfo");
    //   console.log('aaaaa')
    //   console.log(a)
    //   console.log(wx.getStorageSync("userInfo"))
    //   a && 0 != a.memberInfo.uid && "" != a.memberInfo ? (t.setData({
    //     userId: a.memberInfo.uid
    //   })) : wx.getSetting({
    //     success: function (e) {
    //       console.log(e)
    //       console.log(e.authSetting["scope.userInfo"])
    //       false == e.authSetting["scope.userInfo"] ? wx.showModal({
    //         title: "提示",
    //         content: "允许小程序获取您的用户信息后才可查看线索哦",
    //         showCancel: !1,
    //         success: function (e) {
    //           console.log(e.authSetting["scope.userInfo"])
    //           e.confirm && wx.openSetting({
    //             success: function (e) {
    //               1 == e.authSetting["scope.userInfo"] && (t.setData({
    //                 loginModelHidden: !1
    //               }), wx.removeStorageSync("userInfo"));
    //             }
    //           });
    //         }
    //       }) : (wx.removeStorageSync("userInfo"), t.setData({
    //         loginModelHidden: !1
    //       }));
    //       console.log(wx.removeStorageSync("userInfo"))
    //       console.log(t.setData)
    //     }
    //   })
    // },
    // // 获取用户信息
    updateUserInfo: function (e) {
      //缓存中获取用户id
      wx.getStorage({
        key: 'userId',
        success: id => {
          //获取微信用户信息
          wx.getUserInfo({
            success: info => {
              console.info('info',info);
            }
          });
        },
        fail: err => {
          //登录
          wx.login({
            success: login => {
              console.log('login',login);
              this.setData({
                code: login.code
              }); //先获取用户信息

              wx.getUserInfo({
                success: info => {
                  console.info('info',info);
                  console.log('this.code',this.code);
                  this.setData({
                    img: info.userInfo.avatarUrl,
                    name: info.userInfo.nickName
                  });
                  console.log('1',this.name, this.img); //调用后台接口，获取userID
                  wx.request({
                    url: api.default.login,
                    //仅为示例，并非真实的接口地址
                    method: 'POST',
                    data: {
                      code: this.code,
                      nickname: this.name,
                      avatarUrl: this.img ,
											// iv: res1.iv,
											// data: res1.rawData

                    },
                    header: {
                      'content-type': 'application/x-www-form-urlencoded' // 默认值

                    },

                    success(res2) {
                      console.log('res2',res2);
                      wx.setStorage({
                        key: 'openid',
                        data: res2.data.data.openid
                      });
                      wx.setStorage({
                        key: 'userId',
                        data: res2.data.data.id,
                        success: function (res3) {
                          console.log(res3, 'userID');
                        },
                        fail: function (res3) {},
                        complete: function (res3) {}
                      });
                      wx.setStorage({
                        key: 'userName',
                        data: res2.data.data.nickname
                      });
                      wx.setStorage({
                        key: 'userHeadImg',
                        data: res2.data.data.avatar
                      });
                      setTimeout(function () {
                        wx.navigateBack({
                          detail: 1
                        });
                      }, 1500);
                    }

                  });
                }
              });
            }
          });
        }
      }); //   var t = this;
      //   app.util.getUserInfo(function (e) {
      //     console.log(e)
      //     // t.login();
      //     // console.log(userInfo.sessionid)
      //     e = wx.getStorageSync("userInfo");
      //     let userId = wx.getStorageSync("userId")
      //     // console.log(a)
      //     t.setData({
      //       userId: userId,
      //       loginModelHidden: !0
      //     })
      //   }, e.detail);
      //    setTimeout(function () {
      //       wx.navigateBack({
      //          detail: 1
      //       })
      //   },2000)
    }
  }
};
</script>
<style>
@import "./login.css";
</style>